<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
use Storage;

/**
 * Class Commodity
 * @package App
 */
class Commodity extends Model
{
    public $timestamps = false;
    protected $table = 's_shangpin';

    //促销状态
    const CUXIAO_ON = 1;
    const CUXIAO_OFF = 0;

    static $CuxiaoDisplay = [
        self::CUXIAO_ON => '正在促销',
        self::CUXIAO_OFF => '未促销',
    ];

    //热门状态
    const HOT_ON = 1;
    const HOT_OFF = 0;

    static $HotDisplay = [
        self::HOT_ON => '热销中',
        self::HOT_OFF => '未热销',
    ];

    //促销种类
    const CUXIAO_TYPE_NULL = 0;
    const CUXIAO_TYPE_ROB = 3;
    const CUXIAO_TYPE_COLLAGE = 2;

    static $CuxiaoTypeDisplay = [
        self::CUXIAO_TYPE_NULL => '空',
        self::CUXIAO_TYPE_ROB => '抢团',
        self::CUXIAO_TYPE_COLLAGE => '拼团',
    ];

    //推荐指数
    const TUIJIAN_ZERO = 0;
    const TUIJIAN_ONE = 1;
    const TUIJIAN_TWO = 2;
    const TUIJIAN_THIRD = 3;
    const TUIJIAN_FOUR = 4;
    const TUIJIAN_FIVE = 5;
    static $TuijianDisplay = [
        self::TUIJIAN_ZERO => self::TUIJIAN_ZERO,
        self::TUIJIAN_ONE => self::TUIJIAN_ONE,
        self::TUIJIAN_TWO => self::TUIJIAN_TWO,
        self::TUIJIAN_THIRD => self::TUIJIAN_THIRD,
        self::TUIJIAN_FOUR => self::TUIJIAN_FOUR,
        self::TUIJIAN_FIVE => self::TUIJIAN_FIVE,
    ];


    //是否上架
    const ON_OFF_NO = 0;
    const ON_OFF_YES = 1;

    /**
     * 添加商铺
     * @param $request
     * @return array
     */
    static function CommodityPublish($request)
    {
        $publish = new Commodity();
        $publish->uuid = Curl::CreateUuid();
        $publish->title = request('title');
        $publish->store_name = DB::table('s_shanghu')->where('id', request('store_id'))->value('store_name');;
        $publish->store_id = request('store_id');
        $publish->stars = request('stars');
        //促销
        if (request()->has('is_cuxiao')) {
            $publish->is_cuxiao = self::CUXIAO_ON;
        } else {
            $publish->is_cuxiao = self::CUXIAO_OFF;
        }
        $publish->cx_type = request('cx_type');
        //热门
        if (request()->has('is_hot')) {
            $publish->is_hot = self::HOT_ON;
        } else {
            $publish->is_hot = self::HOT_OFF;
        }
        //是否上架
        if (request()->has('on_off')) {
            $publish->on_off = self::ON_OFF_YES;
        } else {
            $publish->on_off = self::ON_OFF_NO;
        }
        $publish->stock_num = request('stock_num');
        $publish->allnum = request('allnum');
        $publish->restrictnum = request('restrictnum');
        $publish->sell_num = request('sell_num');
        $publish->yuan_price = request('yuan_price');
        $publish->xian_price = request('xian_price');
        $publish->cx_id = request('cx_id');

        //图片
        if ($request->has('pic_s')) {
            $publish->pic_s = Storage::url($request->pic_s->store('/', 'oss'));
        }
        if ($request->has('pic_s2')) {
            $publish->pic_s2 = Storage::url($request->pic_s2->store('/', 'oss'));
        }
        if ($request->has('pic_s3')) {
            $publish->pic_s3 = Storage::url($request->pic_s3->store('/', 'oss'));
        }
        if ($request->has('pic1')) {
            $publish->pic1 = Storage::url($request->pic1->store('/', 'oss'));
        }
        if ($request->has('pic2')) {
            $publish->pic2 = Storage::url($request->pic2->store('/', 'oss'));
        }
        if ($request->has('pic3')) {
            $publish->pic3 = Storage::url($request->pic3->store('/', 'oss'));
        }
        if ($request->has('pic4')) {
            $publish->pic4 = Storage::url($request->pic4->store('/', 'oss'));
        }
        if ($request->has('pic5')) {
            $publish->pic5 = Storage::url($request->pic5->store('/', 'oss'));
        }
        if ($request->has('pic6')) {
            $publish->pic6 = Storage::url($request->pic6->store('/', 'oss'));
        }

        $publish->ctime = date('Y-m-d H:i:s', time());
        if (!$publish->save()) {
            return ['status' => 2, 'message' => '添加失败'];
        }
        return ['status' => 1, 'message' => '添加成功'];
    }


    /**
     * 修改商品
     * @param $request
     * @param $commodity_id
     * @return array
     */
    static function CommodityModify($request, $commodity_id)
    {
        $modify = Commodity::find($commodity_id);
        $modify->title = request('title');
        $modify->store_name = DB::table('s_shanghu')->where('id', request('store_id'))->value('store_name');;
        $modify->store_id = request('store_id');
        $modify->stars = request('stars');
        //促销
        if (request()->has('is_cuxiao')) {
            $modify->is_cuxiao = self::CUXIAO_ON;
        } else {
            $modify->is_cuxiao = self::CUXIAO_OFF;
        }
        $modify->cx_type = request('cx_type');
        //热门
        if (request()->has('is_hot')) {
            $modify->is_hot = self::HOT_ON;
        } else {
            $modify->is_hot = self::HOT_OFF;
        }
        //是否上架
        if (request()->has('on_off')) {
            $modify->on_off = self::ON_OFF_YES;
        } else {
            $modify->on_off = self::ON_OFF_NO;
        }
        $modify->stock_num = request('stock_num');
        $modify->allnum = request('allnum');
        $modify->restrictnum = request('restrictnum');
        $modify->sell_num = request('sell_num');
        $modify->yuan_price = request('yuan_price');
        $modify->xian_price = request('xian_price');
        $modify->cx_id = request('cx_id');

        //图片
        if ($request->has('pic_s')) {
            $modify->pic_s = Storage::url($request->pic_s->store('/', 'oss'));
        }
        if ($request->has('pic_s2')) {
            $modify->pic_s2 = Storage::url($request->pic_s2->store('/', 'oss'));
        }
        if ($request->has('pic_s3')) {
            $modify->pic_s3 = Storage::url($request->pic_s3->store('/', 'oss'));
        }
        if ($request->has('pic1')) {
            $modify->pic1 = Storage::url($request->pic1->store('/', 'oss'));
        }
        if ($request->has('pic2')) {
            $modify->pic2 = Storage::url($request->pic2->store('/', 'oss'));
        }
        if ($request->has('pic3')) {
            $modify->pic3 = Storage::url($request->pic3->store('/', 'oss'));
        }
        if ($request->has('pic4')) {
            $modify->pic4 = Storage::url($request->pic4->store('/', 'oss'));
        }
        if ($request->has('pic5')) {
            $modify->pic5 = Storage::url($request->pic5->store('/', 'oss'));
        }
        if ($request->has('pic6')) {
            $modify->pic6 = Storage::url($request->pic6->store('/', 'oss'));
        }
        if (!$modify->save()) {
            return ['status' => 2, 'message' => '修改失败'];
        }
        return ['status' => 1, 'message' => '修改成功'];
    }

}
